const network_utils = require('../../../http/NetworkUtils.js');
const constants = require('../../../common/Constants.js');
import YesterdayList from '../../../templates/YesterdayList/YesterdayList.js'

var page = null;
var app = getApp();
let queryContent = null;

const FromPages = {
    OVERVIEW: "overview",
    QUERY_MERCHANTS: "queryMerchants",
    SEARCH_AGENT_STORES: "searchStores"
};

Page(Object.assign({
    data: {
        //选中的index
        currtabIndex: 1, 
        //降序为true
        currtabState: true,
        swipertab: [{name: '交易量', index: 1}, {name: '交易笔数', index: 2}, {name: '部署时间', index: 3},],
        tradingVolume: [],
        hasMore: false
    },

    onLoad: function (options) {
        if (!app.globalData.systemInfo.deviceHeight) {
            app.globalData.systemInfo.deviceHeight = wx.getSystemInfoSync().windowHeight;
        }
        // 页面初始化 options为页面跳转所带来的参数
        let params = JSON.parse(options.params);
        page = 0;
        this.setData({
            deviceW: app.globalData.systemInfo.windowWidth,
            deviceH: app.globalData.systemInfo.deviceHeight,
            // fromPage: options.optionsData ? 'searchStores' : ''
            fromPage: params.fromPage
        });

        // if (params.fromPage === FromPages.SEARCH_AGENT_STORES) {
            //查询店铺
        queryContent = JSON.parse(params.optionsData ? params.optionsData:"{}") ;
            this._searchStoresRequest();
        // } else {
        //     this._netWorkRequest();
        // }
    },

    /**
     * @Explain：选项卡点击切换
     */
    tabSwitch: function (e) {
        page = 0;
        if (this.data.currtabIndex === e.currentTarget.dataset.current) {
            this.setData({
                currtabState: !this.data.currtabState,
                tradingVolume: [],
                hasMore: false
            })
        } else {
            this.setData({
                currtabState: true,
                currtabIndex: e.currentTarget.dataset.current,
                tradingVolume: [],
                hasMore: false
            })
        }
        //网络请求
        this.doRequest();
    },

    //加载更多
    loadMore: function () {
        this.doRequest();
    },

    doRequest() {
        // if (this.data.fromPage === FromPages.SEARCH_AGENT_STORES) {
            this._searchStoresRequest();
        // } else {
        //     this._netWorkRequest();
        // }
    },

    _netWorkRequest: function () {
        let that = this;
        let params = {
            order: that.data.currtabIndex.toString(),
            flag: that.data.currtabState ? "2" : "1",
            assistId: app.globalData.userData.assistId,
            pageNo: page + 1 + '',
            pageSize: app.globalData.pageSize,
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_Merchant_YesterdayTrading, params, {

            doHttpSucess: function (data) {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    page++;
                    that._dataFormate(data["storeList"]);
                    return true;
                }

                return false;
            },
        }).send();
    },

    //SAAS 三期   代理商商户查询
    _searchStoresRequest: function () {
        let that = this;
        let params = {
            order: that.data.currtabIndex.toString(),
            flag: that.data.currtabState ? "2" : "1",
            agentId: queryContent.agentId ? queryContent.agentId:app.globalData.userData.agentId,
            storeStatus: queryContent.storeStatus ? queryContent.storeStatus:"3",
            storeName: queryContent.storeName,
            startDate: queryContent.startDate,
            endDates: queryContent.endDates,
            pageNo: page + 1 + '',
            pageSize: app.globalData.pageSize,
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_SEARCH_STORES, params, {
            doHttpSucess: function (data) {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    page++;
                    that._dataFormate(data["storeList"]);
                    return true;
                }

                return false;
            },
        }).send();
    },

}, YesterdayList));
